package com.zhongshengnetwork.rightbe.lang.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.customview.FlowLayout;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.LangTypeModel;
import com.zhongshengnetwork.rightbe.lang.view.ZFlowLayout;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LangSearchActivity extends AppBaseActivity {

    private FlowLayout flowLayout;
    private String[] mLabels = {"名人名言","名人故事","读懂暗语","诗词改编","口才交际", "时尚热点", "新颖观点", "生活感悟", "创新思维", "面试问答","励志感人","词语解说","教育启蒙","幽默诙谐","经典台词"};
    private List<String>langNameList;
    private HashMap<String, String> langNameMap;
    private EditText search_edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lang_search);
        search_edit = (EditText) findViewById(R.id.search_edit);
        search_edit.setHintTextColor(AppThemeUtils.getInstance().getLightTextColor());
        search_edit.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        search_edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    String search = search_edit.getText().toString().trim();
                    onClickSearch(textView);
                    return true;
                }
                return false;
            }
        });
        RelativeLayout lang_search_layout = (RelativeLayout) findViewById(R.id.lang_search_layout);
        lang_search_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        RelativeLayout search_top = (RelativeLayout) findViewById(R.id.search_top);
        search_top.setBackgroundColor(AppThemeUtils.getInstance().getSearchBgColor());
        RelativeLayout search_shape_layout = (RelativeLayout) findViewById(R.id.search_shape_layout);
        search_shape_layout.setBackgroundResource(AppThemeUtils.getInstance().getSearchBackground());
        initType();
    }

    private void initType(){
        flowLayout = (FlowLayout) findViewById(R.id.mflowLayout);
        langNameList = new ArrayList<String>();
        langNameMap = new HashMap<String, String>();
        String data = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.LangTypeName,"");
        if (CommonUtils.isEmpty(data)){
            for (int i = 0; i < mLabels.length; i++){
                langNameList.add(mLabels[i]);
                langNameMap.put(mLabels[i],(i+1)+"");
            }
            search_edit.setText("文案|短句|正能量|早安|晚安");
        }else {
            search_edit.setHint(GsonTools.getLangSearchText(data));
            List<LangTypeModel>l = GsonTools.getLangTypeModel(data);
            if (l != null && l.size() > 0){
                Log.e("TAG","网络请求的数据\n");
                for (int i = 0; i < l.size(); i++){
                    LangTypeModel m = l.get(i);
                    langNameList.add(m.getName());
                    langNameMap.put(m.getName(),m.getId()+"");
                }
            }
        }
        flowLayout.setColorful(false);
        String[] strArrayTrue = (String[]) langNameList.toArray(new String[0]);
        flowLayout.setData(strArrayTrue);
        flowLayout.setOnTagClickListener(new FlowLayout.OnTagClickListener() {
            @Override
            public void TagClick(String text) {
                int type = 0;
                String title = text;
                String id = langNameMap.get(title);
                if (!CommonUtils.isEmpty(id)){
                    type = Integer.valueOf(id);
                }
                Intent intent = new Intent(LangSearchActivity.this, LangSearchResultActivity.class);
                intent.putExtra("type",type);
                intent.putExtra("title",title);
                intent.putExtra("fromsearch", true);
                startActivity(intent);
            }
        });
    }

    public void onClickBack(View view){
        back();
    }

    @Override
    public void onBackPressed()
    {
        Log.e("TAG","按下返回键了");
        back();
    }

    private void back(){
        hintKeyBoard();
        finish();
    }

    public void onClickSearch(View view){
        String text = search_edit.getText().toString().trim();
        if (text.length() == 0){
            ToastUtil.show(LangSearchActivity.this,"请输入搜索内容");
            return;
        }
        Intent intent = new Intent(LangSearchActivity.this, LangSearchResultActivity.class);
        intent.putExtra("type",0);
        intent.putExtra("title",text);
        intent.putExtra("fromsearch", true);
        startActivity(intent);
    }

    public void hintKeyBoard() {
        //拿到InputMethodManager
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        //如果window上view获取焦点 && view不为空
        if (imm.isActive() && getCurrentFocus() != null) {
            //拿到view的token 不为空
            if (getCurrentFocus().getWindowToken() != null) {
                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

}
